// @ts-check
const { defineConfig } = require("socg")

module.exports = defineConfig({
    generate: {
        output(code) {
            return [
                `import request from "@/utils/request";`,
                `import type { DataExtact } from "@/api/model";`,
                code
            ].join('\r\n')
        },
        template({URL, METHOD, QUERY, BODY, RESPONSE, REQUESTCONTENTTYPE}) {
            if (METHOD === "get" || METHOD === "delete") {
                if (QUERY) {
                    return `request.${METHOD}<unknown, DataExtact<${RESPONSE}>>(${URL}, {
                      params: ${QUERY}
                    })`
                }
                return `request.${METHOD}<unknown, DataExtact<${RESPONSE}>>(${URL})`
            } else {
                const contentType = REQUESTCONTENTTYPE === "form_data" ? "multipart/form-data" : "application/json"
                if (BODY) {
                    return `request.${METHOD}<unknown, DataExtact<${RESPONSE}>>(${URL}, ${BODY}, {
                        headers: {
                          "Content-Type": "${contentType}"
                        }
                    })`
                }
                return `request.${METHOD}<unknown, DataExtact<${RESPONSE}>>(${URL})`
            }
        },
        dir: "src/api/designer",
        model: "model.ts",
        locale: "zh-CN",
        allowImportingTsExtensions: false,
        verbatimModuleSyntax: false,
        filterTag: ['Designer'],
    },
    customEnumMember(enumValue) {
        if (enumValue.toString().indexOf('=') !== -1) {
            const [name, initializer] = enumValue.toString().split('=')
            const value = Number(initializer)
            return {
                name,
                initializer: Number.isNaN(value) ? initializer : value
            }
        }
    }
})
